<?php
/**
 *  
 *
  */
namespace Org\Util;
class Jsgxk3 {

    
     public function gxk3_balance() {
		$condition['c_id']=7;
		$condition['status']=0;
 		$order=M('lottery_order')->where($condition)->select();  //所有没有结算订单
		
		$period=M('lottery_order')->distinct(true)->field('c_no')->where($condition)->select();  //没结算期数
		foreach($period as $k=>$v){
 		  $condition1['c_id']=7;
		  $condition1['c_no']=$v['c_no'];
		  $cqssc_result[$v['c_no']]=M('lottery_results')->where($condition1)->getField('lottery_result');  //根据期数获取开奖结果
 		}
		 
		foreach($order as $ok=>$ov){
 		   if($cqssc_result[$ov['c_no']]){    //有开奖结果才结算
 		      $this->jiesuan($cqssc_result[$ov['c_no']],$ov);
			  }
 		}
		return; 		
     }
	
	 public function jiesuan($result,$info=array())
	 {
		 
		 $num= explode(",",$result);
		 $sum=array_sum($num);
 		 
		 //总和大小玩法 DX
		 if($info['w_type']=='DX'){
 			 $this->zh_dx($num,$info);
			 }
		 //三军/鱼虾蟹 3G
		 if($info['w_type']=='3G'){
 			 $this->k_3g($num,$info);
			 }
		 //围骰 WS	 
  		 if($info['w_type']=='WS'){
 			 $this->k_ws($num,$info);
			 }
		//全骰 QS 
  		 if($info['w_type']=='QS'){
 			 $this->k_qs($num,$info);
			 }	
		//点数 DS
		if($info['w_type']=='DS'){
 			 $this->k_ds($sum,$info);
			 }
		//长牌 CP
		if($info['w_type']=='CP'){
 			 $this->k_cp($num,$info);
			 }	
		//短牌 DP
		if($info['w_type']=='DP'){
 			 $this->k_dp($num,$info);
			 }	  	  
 	  }
		 
		 
	  public function zh_dx($num,$info){  //总和大小
	      $sum1=array_sum($num);
		  $flag=0;
          if($sum1>=11){
		     $dx='D';	 
			}else{
			 $dx='X';	 
			}
		  if($num[0]==$num[1]&&$num[0]==$num[2]){
			$flag=1;  
			  }	
	      if($dx==$info['t_type']&&$flag!=1){  //中奖  三个号码一样 大小通吃
			  $this->zhongjiang($info);
 			  }else{
			  //不中奖  判断是否反水
 			     $this->fanshui($info);
				 
			 }
       }
	   
	   public function k_3g($num,$info){     //三军/鱼虾蟹 3G
		   
		    if (in_array($info['t_type'], $num)){
				$this->zhongjiang($info);
				}else{
			    //不中奖  判断是否反水
 			    $this->fanshui($info);
			 }
		   
	   }
	   
	   public function k_ws($num,$info){     //围骰 WS
	      $result=$num[0].$num[1].$num[2];
		  if($result==$info['t_type']){
			$this->zhongjiang($info);
				}else{
			    //不中奖  判断是否反水
 			    $this->fanshui($info);
			 }
 	   }
	   public function k_qs($num,$info){     //全骰 QS
		   if($num[0]==$num[1]&&$num[0]==$num[2]){
			  $str=0;
			   }
		   if($str==$info['t_type']){
			$this->zhongjiang($info);
				}else{
			    //不中奖  判断是否反水
 			    $this->fanshui($info);
			 }
	   }
	   //点数
	   public function k_ds($sum,$info){
		   if($sum==$info['t_type']){
			$this->zhongjiang($info);
			  }else{
			  //不中奖  判断是否反水
			  $this->fanshui($info);
		   }
	   }
	   //长牌
	   public function k_cp($num,$info){
		  $arr1 = str_split($info['t_type']);
		  $is_w=array_intersect($num, $arr1);
		  $count=count($is_w);
		  if($count==2){
			$this->zhongjiang($info);
				}else{
			    //不中奖  判断是否反水
 			    $this->fanshui($info);
			 }
	   }
       //短牌
	   public function k_dp($num,$info){
		  $arr1 = str_split($info['t_type']);
		  $is_w=array_intersect($num, $arr1);   //数组交集
		  $count=count($is_w);
		  if($count==2){
			$this->zhongjiang($info);
				}else{
			    //不中奖  判断是否反水
 			    $this->fanshui($info);
			 }
	   }
 
  
 public function zhongjiang($info){
 	$money=round((float)$info['money']*$info['odds'],2);
	//print_r($money);exit;
	$updata_data['is_winning'] = 1;
	$updata_data['status'] = 1;
	$updata_data['js_time'] = time();
	$updata_data['win_money'] =round((float)$info['money']*($info['odds']-1),2);
	M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
	//添加余额变动记录
	$insert_log['m_id']=$info['m_id'];
	$insert_log['change_money']=$money;
	$insert_log['change_type']=5;
	$insert_log['add_time']=time();
	$insert_log['remark']='广西快3中奖 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
	M('account_log')->add($insert_log);
	 }
	 
 public function fanshui($info){
 	  $updata_data['is_winning'] = 2;
	  $updata_data['status'] = 1;
	  $updata_data['js_time'] = time();
	 // M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
		if($info['rebate']>0){
			M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$info['rebate']); // 用户的余额增加
			//添加余额变动记录
			$insert_log['m_id']=$info['m_id'];
			$insert_log['change_money']=$info['rebate'];
			$insert_log['change_type']=2;
			$insert_log['add_time']=time();
			$insert_log['remark']='广西快3反水 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
			M('account_log')->add($insert_log);
		}
		 $updata_data['lost_money'] =$info['money']-$info['rebate'];
		  M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	 }	 
	 
}